#!/bin/bash

CLIENT=$1
CSTORE=$2

MAINDIR=$PWD

cd /etc/openvpn/easy-rsa/
./easyrsa build-client-full $CLIENT nopass
cp /etc/openvpn/client-common.txt $CSTORE/$CLIENT.ovpn
echo "<ca>" >> $CSTORE/$CLIENT.ovpn
cat /etc/openvpn/easy-rsa/pki/ca.crt >> $CSTORE/$CLIENT.ovpn
echo "</ca>" >> $CSTORE/$CLIENT.ovpn
echo "<cert>" >> $CSTORE/$CLIENT.ovpn
cat /etc/openvpn/easy-rsa/pki/issued/$CLIENT.crt >> $CSTORE/$CLIENT.ovpn
echo "</cert>" >> $CSTORE/$CLIENT.ovpn
echo "<key>" >> $CSTORE/$CLIENT.ovpn
cat /etc/openvpn/easy-rsa/pki/private/$CLIENT.key >> $CSTORE/$CLIENT.ovpn
echo "</key>" >> $CSTORE/$CLIENT.ovpn
echo "<tls-auth>" >> $CSTORE/$CLIENT.ovpn
cat /etc/openvpn/ta.key >> $CSTORE/$CLIENT.ovpn
echo "</tls-auth>" >> $CSTORE/$CLIENT.ovpn

cd $MAINDIR

echo "Restarting Server..."

./bin/stop
./bin/start